﻿using System;
using System.Web.UI.WebControls;
using DMS.BaseClass;
using DMS.Business.Modules.ArchivesManagement;
using DMS.Business.Modules.Organization;
using DMS.Data;
using DMS.Business.Utility;

namespace DMS.Web.Modules.ArchivesManagement
{
    public partial class ArchInfoForm : System.Web.UI.Page
    {
        /// <summary>
        /// 表单ID
        /// </summary>
        private string _id = "";
        /// <summary>
        /// 表单ID
        /// </summary>
        private string _baseInfoId = "";
        /// <summary>
        /// 操作类型，包括New/Edit/View
        /// </summary>
        private ActionType _action;

        public string Title="";

        private static User _currentUser = SystemContext.Current.LoginUser;

        protected void Page_Load(object sender, EventArgs e)
        {
            _id = Request["id"];
            _baseInfoId = Request["baseInfoId"];
            _action = (ActionType)Enum.Parse(typeof(ActionType), Request["action"]);
            this.AttachmentList.ArchivesID = _id;
            if (!IsPostBack)
            {
                InitControls();
                InitData();
            }
        }

        private void InitControls()
        {
            LoadAttachmentList();

            int now = DateTime.Now.Year;

            if (string.IsNullOrEmpty(_id))
            {
                foreach (ListItem item in this.BelongYear.Items)
                {
                    if (item.Value == now.ToString())
                    {
                        item.Selected = true;
                    }
                }
            }

            if (!SystemContext.Current.LoginUser.IsInRole("档案管理员;系统管理员;"))
            {
                this.btnSave.Visible = false;
            }
        }

        private void InitData()
        {
            if (_id != "")
            {
                AM_ArchivesInfo archInfo = ArchInfoService.GetArchInfoById(_id);
                FormBinder.BindObjectToControls(archInfo, divContent);
                this.ArchType.SelectedValue = archInfo.Type;

                Title = archInfo.Name;

                if (archInfo.CustodyYear == 0)
                {
                    this.Yongjiu.Text = "永久";
                }
                if (archInfo.InitDate != null)
                {
                    this.InitDate.Text = ((DateTime) archInfo.InitDate).ToString("yyyy-MM-dd");
                }
                if (archInfo.InputDate != null)
                {
                    this.InputDate.Text = DateTime.Parse(archInfo.InputDate).ToString("yyyy-MM-dd");
                }
                if (archInfo.CustodyBeginDate != null)
                {
                    this.CustodyBeginDate.Value = ((DateTime) archInfo.CustodyBeginDate).ToString("yyyy-MM-dd");
                    this.CustodyBeginDateL.Text = ((DateTime) archInfo.CustodyBeginDate).ToString("yyyy-MM-dd");
                }
                if (archInfo.CustodyEndDate != null)
                {
                    this.CustodyEndDate.Value = ((DateTime) archInfo.CustodyEndDate).ToString("yyyy-MM-dd");
                    this.CustodyEndDateL.Text = ((DateTime) archInfo.CustodyEndDate).ToString("yyyy-MM-dd");
                }
                if(archInfo.InputPersonID==null)
                {
                    Department _currentBigDept = _currentUser.BigDept;
                    this.InputPersonID.Value = _currentUser.ID;
                    this.InputPersonName.Text = _currentUser.Name; 
                    this.DeptName.Value = _currentBigDept.Name; 
                    this.DeptID.Value = _currentBigDept.ID; 
                }
                
            }
            else
            {
                Department _currentBigDept = _currentUser.BigDept;
                this.ArchivesID.Value = Guid.NewGuid().ToString();
                this.BaseInfoID.Value = _baseInfoId;
                this.InputDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.InputPersonID.Value = _currentUser.ID;
                this.InputPersonName.Text = _currentUser.Name; 
                this.DeptName.Value =_currentBigDept.Name; 
                this.DeptID.Value = _currentBigDept.ID; 
            }
        }

        private void LoadAttachmentList()
        {
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            try
            {
                AM_ArchivesInfo archInfo;
                if (_action == ActionType.New)
                {
                    archInfo = new AM_ArchivesInfo();
                }
                else
                {
                    archInfo = ArchInfoService.GetArchInfoById(_id);
                }
                FormBinder.BindControlsToObject(archInfo, divContent);

                archInfo.Type = this.ArchType.SelectedValue;
                archInfo.CustodyBeginDate = DateTime.Parse(this.CustodyBeginDate.Value);
                if(archInfo.CustodyYear!=0)
                {
                    archInfo.CustodyEndDate = DateTime.Parse(this.CustodyEndDate.Value);
                }

                archInfo.InputDate = this.InputDate.Text;
                
                archInfo.InputPersonID = _currentUser.ID;
                archInfo.InputPersonName = _currentUser.Name;

                ArchInfoService.SaveOrUpdateArchInfo(archInfo);

                ClientHelper.Instance.CloseWindow("保存成功", "ok", RefreshType.OpenWindow);
            }
            catch (Exception ex)
            {
                string code = LogHelper.LogException(ex);
                string msg = string.Format("操作失败，请联系管理员。\\r\\n错误码：{0}", code);
                ClientHelper.Instance.Alert(msg);
            }
        }

        protected void ProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //List<SYS_Address_City> list = AddressService.GetCitysByProvinceId(ProvinceID.SelectedValue);
            //CityID.DataSource = list;
            //CityID.DataBind();

            //foreach (ListItem item in CityID.Items)
            //{
            //    if (item.Text == "广州市")
            //    {
            //        item.Selected = true;
            //        CityID_SelectedIndexChanged(null, null);
            //        break;
            //    }
            //}
        }

        protected void CityID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //List<SYS_Address_District> list = AddressService.GetDistrictsByCityId(CityID.SelectedValue);
            //DistrictID.DataSource = list;
            //DistrictID.DataBind();
        }
    }
}